package com.hongning.railway.ui.main.mine.partymember;

import android.app.Application;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableList;
import android.support.annotation.NonNull;
import android.util.Log;

import com.hongning.railway.BR;
import com.hongning.railway.R;
import com.hongning.railway.data.AppRepository;
import com.hongning.railway.entity.reqbean.QuestionRequestListBean;
import com.hongning.railway.entity.reqbean.QuestionResultBean;
import com.hongning.railway.entity.reqbean.QuestionResultListBean;
import com.hongning.railway.entity.reqbean.ReqAssessesIntegral;
import com.hongning.railway.ui.main.mine.partymember.itemviewmodel.CheckResultItemViewModel;
import com.hongning.railway.ui.main.mine.partymember.itemviewmodel.QuestionItemViewModel;
import com.hongning.railway.util.Apputils;

import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.http.ApiDisposableObserver;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class CheckReultFragmentViewModel extends BaseViewModel<AppRepository> {

    public ItemBinding<CheckResultItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_check_result);
    public ObservableList<CheckResultItemViewModel> viewlist = new ObservableArrayList<>();


    public CheckReultFragmentViewModel(@NonNull Application application) {
        super(application);

    }



    public CheckReultFragmentViewModel(@NonNull Application application, AppRepository model) {
        super(application, model);
    }

    public void requestData(QuestionRequestListBean questionRequestListBean){
        questionRequestListBean.setPageNum("1");
        questionRequestListBean.setPageSize("10000");
        questionRequestListBean.setStaffId(Apputils.getUserId());
        model.selectStatisticsByPage(questionRequestListBean)
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        showDialog();
                    }
                }).doOnTerminate(new Action() {
            @Override
            public void run() throws Exception {
                dismissDialog();
            }
        }).subscribe(new ApiDisposableObserver<QuestionResultListBean>() {
            @Override
            public void onResult(QuestionResultListBean questionResultListBeans) {
       if (questionResultListBeans==null|| questionResultListBeans.getSelectResult()==null||questionResultListBeans.getSelectResult().isEmpty())
        {
            ToastUtils.showShort("无数据");
            return;
        }
       if (viewlist!=null)
       {
           viewlist.clear();
       }
        for (QuestionResultListBean.SelectResultBean selectResultBean:questionResultListBeans.getSelectResult())
        {
            viewlist.add(new CheckResultItemViewModel(CheckReultFragmentViewModel.this,selectResultBean));
        }
            }
        });



    }
}
